package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.project.bean.SupplierBean;
import com.project.bean.SupplierExcelBean;
import com.project.service.ISupplierService;
import com.project.service.impl.SupplierServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
@RequiresPermissions("supplier")
@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;

    @RequestMapping("findAll")//查询所有供应商返回供应商集合
    public List<SupplierBean> findAll() {
        return supplierService.findAll();
    }

    @RequestMapping("findByItem")//根据输入的供应商名称，经营的范围动态查询，返回供应商分页对象
    public IPage<SupplierBean> findByItem(Integer pageNO,
                                          String name, String scope) {
//        System.out.println("id的值是"+pageNO);
        return supplierService.findByItem(pageNO, name, scope);
    }

    @RequestMapping("findById")//按照供应商id查询供应商信息
    public SupplierBean findById(Integer id) {
        return supplierService.findById(id);
    }

    @RequestMapping("updateInfo")//根据供应商id修改供应商负责人名字和电话号码
    public String updateInfo(Integer id, String director, String phone) {
        supplierService.updateInfo(id, director, phone);
        return "ok";
    }

    @RequestMapping("add")//添加供应商，* 如果供应商已存在，则不允许添加。
    public String add(SupplierBean supplierBean) {
//        System.out.println("添加"+supplierBean);
        boolean flag = supplierService.add(supplierBean);
        if (flag != false) {
            return "ok";
        }
        return "no";
    }

    @RequestMapping("addExcel")//添加供应商excel表
    public String add(MultipartFile excelFile) throws Exception {
//        System.out.println("得到"+excelFile);
        if (excelFile != null) {            //设置初始化信息
            ImportParams params = new ImportParams();
            //设置选项卡
            params.setSheetNum(1);
            //设置标题栏占用的行数
            params.setTitleRows(1);
            //设置表头占用的行数
            params.setHeadRows(1);
            //读取上传文件信息，封装为实体对象集合
            //第一个参数为上传文件的二进制流，第二个参数为封装实体类的类模板，第三个参数为初始化信息
            List<SupplierBean> list = ExcelImportUtil.importExcel(excelFile.getInputStream(), SupplierBean.class, params);
            supplierService.addExcel(list);
            return "ok";
        }
        return "no";
    }

}
